﻿package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.ui.Mouse;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	
	public class RectView extends Sprite{
		
		//矩形选择框容器
		private var rectSprite:Sprite;
		
		//缩放触发容器
		private var zoomSprite:Sprite;
		
		//拖放计时器
		private var t:Timer;
		
		//缩放图标
		private var zoom_mc:Zoom_MC = new Zoom_MC();
		
		//拖曳图标
		private var drag_mc:Drag_MC = new Drag_MC();
		
		//缩放比例，应为矩形的原始宽度
		private var _zoomScale:Number;
		
		//矩形宽和高 坐标
		public var r_w:int;
		public var r_h:int;
		public var r_x:int;
		public var r_y:int
		
		//移动范围
		public var d_x:int;
		public var d_y:int;
		public var d_w:int;
		public var d_h:int;
		
		private var i:int = 0;
		
		public function RectView(w:int, h:int){
			r_w = w;
			r_h = h;
			init(w,h);
			
		}
		
		//初始化
		private function init(w:int, h:int):void{
			
			
			
			//构造矩形
			rectSprite = new Sprite();
			rectSprite.graphics.beginFill(0xffffff,0.1);
			rectSprite.graphics.lineStyle(0.5, 0, 1)
			/*rectSprite.graphics.moveTo(0, 0);
			rectSprite.graphics.lineTo(70, 0);
			rectSprite.graphics.lineTo(70,80);
			rectSprite.graphics.lineTo(0, 80);
			rectSprite.graphics.lineTo(0, 0);*/
			rectSprite.graphics.drawRect(0,0,w,h);
			rectSprite.graphics.endFill();
			rectSprite.x = (350 - w)/2;
			rectSprite.y = (350 - h)/2;
			addChild(rectSprite);
			
			zoomSprite = new Sprite();
			zoomSprite.graphics.beginFill(0x000000,0.3);
			zoomSprite.graphics.lineStyle(0.5, 0, 1);
			zoomSprite.graphics.moveTo(w, h * 0.85);
			zoomSprite.graphics.lineTo(w * 0.85, h);
			zoomSprite.graphics.lineTo(w, h);
			zoomSprite.graphics.lineTo(w, h * 0.85);
			zoomSprite.graphics.endFill();
			rectSprite.addChild(zoomSprite);
			
			rectSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			//rectSprite.addEventListener(MouseEvent.MOUSE_MOVE,onMoveHandler);
			rectSprite.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			rectSprite.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
			//stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			
			zoomSprite.addEventListener(MouseEvent.MOUSE_DOWN, zoomHandler);
			zoomSprite.addEventListener(MouseEvent.MOUSE_MOVE, onMoveZoomHandler);
			zoomSprite.addEventListener(MouseEvent.MOUSE_OUT, onOutZoomHandler);
			
			t = new Timer(0);
			t.addEventListener(TimerEvent.TIMER, timeHandler);
			
			_zoomScale = rectSprite.width;
			//trace("init");
			
			addChild(zoom_mc);
			
			addChild(drag_mc);
			
			//两个Sprite叠加，MouseEvent会有冲突
			drag_mc.mouseChildren = false;
			drag_mc.mouseEnabled=false;
			zoom_mc.mouseEnabled=false;
			zoom_mc.mouseChildren = false;
			drag_mc.visible = false;
			zoom_mc.visible = false;
			
		}
		
		public function set setX(dx:int):void{
			d_x = dx;
		}
		public function set setY(dy:int):void{
			d_y = dy;
		}
		public function set setW(dw:int):void{
			d_w = dw;
		}
		public function set setH(dh:int):void{
			d_h = dh;
		}
		
		//拖动
		private function onStartDrag(event:MouseEvent):void{
			
			event.updateAfterEvent();
			rectSprite.startDrag(false,new Rectangle(d_x, d_y, d_w, d_h));
			
			//trace("startdrag");
			
		}
		
		//停止拖动
		private function onStopDrag(event:MouseEvent):void{
			rectSprite.stopDrag();
			
			zoom_mc.visible = false;
			Mouse.show();
			//trace("stop");
			//rectSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			
			//rectSprite.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			//rectSprite.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveStageHandler);
			
		}
		
		private function onMoveHandler(event:MouseEvent):void{
			event.updateAfterEvent();
			
			drag_mc.x = mouseX;
			drag_mc.y = mouseY;
			
		}
		
		//鼠标滑过
		private function onOverHandler(event:MouseEvent):void{
			event.updateAfterEvent();
			Mouse.hide();
			t.start();
			drag_mc.visible = true;
			zoom_mc.visible = false;
			//drag_mc.x = mouseX;
			//drag_mc.y = mouseY;
			stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			//trace("over");
			//trace(i++);
		}
		
		//鼠标划出
		private function onOutHandler(event:MouseEvent):void{
			Mouse.show();
			drag_mc.visible = false;
			zoom_mc.visible = false;
			//t.stop();
		}
		
		//缩放
		private function zoomHandler(event:MouseEvent):void{
			event.updateAfterEvent();
			rectSprite.stopDrag();
			zoom_mc.visible = true;
			//rectSprite.removeEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			//rectSprite.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			//rectSprite.removeEventListener(MouseEvent.MOUSE_OUT, onOutHandler)
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveStageHandler);
			
		}
		
		//鼠标跟随
		private function timeHandler(event:TimerEvent):void{
			
			drag_mc.x = mouseX;
			drag_mc.y = mouseY;
			r_x = rectSprite.x;
			r_y = rectSprite.y;
			//zoom_mc.x = mouseX;
			//zoom_mc.y = mouseY
		}
		
		
		
		//鼠标滑过
		private function onMoveZoomHandler(event:MouseEvent):void{
			Mouse.hide();
			zoom_mc.visible = true;
			drag_mc.visible = false;
			zoom_mc.x = mouseX;
			zoom_mc.y = mouseY
			rectSprite.removeEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			rectSprite.removeEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			rectSprite.removeEventListener(MouseEvent.MOUSE_OUT, onOutHandler)
			//t.start();
		}
		
		private function onOutZoomHandler(event:MouseEvent):void{
			Mouse.show();
			drag_mc.visible = false;
			zoom_mc.visible = false;
			rectSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			rectSprite.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			rectSprite.addEventListener(MouseEvent.MOUSE_OUT, onOutHandler);
		}
		

		//鼠标缩放
		private function onMoveStageHandler(event:MouseEvent):void{
			//trace(mouseX);
			event.updateAfterEvent();
			var dx:Number = mouseX - rectSprite.x;
			
			
				rectSprite.scaleX = dx / _zoomScale;
				rectSprite.scaleY = rectSprite.scaleX;
			
			
			Mouse.hide();
			zoom_mc.visible = true;
			zoom_mc.x = mouseX;
			zoom_mc.y = mouseY;
			
			r_w = rectSprite.width;
			r_h = rectSprite.height;
			
			trace(rectSprite.scaleX)
		}
	}
}